package com.wudi.estate.controller.ownermanagement;

import com.wudi.estate.bean.communitymanagement.Community;
import com.wudi.estate.bean.homemanage.Buildings;
import com.wudi.estate.bean.ownermanagement.MemberType;
import com.wudi.estate.bean.ownermanagement.Personnel;
import com.wudi.estate.mapper.ownermanagement.PersonnelMapper;
import com.wudi.estate.service.communitymanagement.CommunityService;
import com.wudi.estate.service.homemanage.BuildingsService;
import com.wudi.estate.service.ownermanagement.MemberTypeService;
import com.wudi.estate.service.ownermanagement.PersonnelService;
import com.wudi.estate.util.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.info.ProjectInfoProperties;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@RequestMapping("/personnel")
@SessionAttributes(value = "buildings")
@Controller
public class PersonnelController {
    @Autowired
    private PersonnelService personnelService;
    @Autowired
    private MemberTypeService memberTypeService;

    @Autowired
    private BuildingsService buildingsService;
    @Autowired
    private CommunityService communityService;
    @RequestMapping("/listUi")
    public String personnel_listUi(String somename,ModelMap map){

        if(somename==null){
            try {
                List<Personnel> personnels = personnelService.selectAll();
                List<Map<String,Object>> pers=insertList(personnels);
                map.addAttribute("pers",pers);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }else{
            try {
                List<Personnel> personnels = personnelService.selectByName(somename);
                List<Map<String,Object>> pers=insertList(personnels);
                map.addAttribute("pers",pers);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return "personnel_list";
    }
    public List<Map<String,Object>> insertList(List<Personnel> personnels) throws Exception {
        List<Map<String,Object>> pers = new ArrayList<>();
        for (Personnel p: personnels) {
            Map<String,Object> map1 = new HashMap<>();
            MemberType m = new MemberType();
            m.setId(p.getMtypeid());
            String s = memberTypeService.selectMemberById(m);
            Buildings buildings = new Buildings();
            buildings.setId(p.getBid());
            buildings = buildingsService.selectById(buildings);
            Community community = communityService.selectById(buildings.getCid());
            map1.put("id",p.getId());
            map1.put("bid",p.getBid());
            map1.put("bname",buildings.getRemark());
            map1.put("community",community);
            map1.put("name",p.getName());
            map1.put("img",p.getImg());
            map1.put("peopleNum",p.getPeopleNum());
            map1.put("contactWay",p.getContactWay());
            map1.put("occupation",p.getOccupation());
            map1.put("birthday",p.getBirthday());
            map1.put("sex",p.isSex());
            map1.put("remark",p.getRemark());
            map1.put("createTime",p.getCreateTime());
            map1.put("state",p.isState());
            map1.put("mname",s);
            pers.add(map1);
        }
        return pers;
    }
    @RequestMapping("/addUi")
    public String personnel_addUi(ModelMap map){
        List<Map<String,Object>> buildings=new ArrayList<>();
        try {
            List<Buildings> buildingsList = buildingsService.selectAll();
            for (Buildings b:buildingsList){
                Map<String,Object> m = new HashMap<>();
                Community community = communityService.selectById(b.getCid());
                m.put("buildings",b);
                m.put("community",community);
                buildings.add(m);
            }
            map.addAttribute("buildings",buildings);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return "personnel_add";
    }
    @RequestMapping("/add")
    public String personnel_add(Personnel personnel, MultipartFile headerFile){

        String fileName = "";
        System.out.println(headerFile);

        try {
            fileName = FileUtils.fileUpload(headerFile);


        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        personnel.setImg(fileName);
        personnel.setCreateTime(new Date());
        try {
            personnelService.insert(personnel);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return "redirect:/personnel/listUi";
    }
    @RequestMapping("/update")
    public String personnel_updateState(long id){
        Personnel p = new Personnel();
        p.setId(id);
        try {
            personnelService.updateStateById(p);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "redirect:/personnel/listUi";
    }
    @RequestMapping("/editUi")
    public String personnel_editUi(long  id,ModelMap map){
        List<Map<String,Object>> buildings=new ArrayList<>();
        try {
            List<Buildings> buildingsList = buildingsService.selectAll();
            for (Buildings b:buildingsList){
                Map<String,Object> m = new HashMap<>();
                Community community = communityService.selectById(b.getCid());
                m.put("buildings",b);
                m.put("community",community);
                buildings.add(m);
            }
            map.addAttribute("buildings",buildings);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        Personnel p = new Personnel();
        p.setId(id);
        Personnel personnel=new Personnel();
        Buildings buildings1 = new Buildings();
        Community community = new Community();
        try {
            personnel = personnelService.selectById(p);
            buildings1= buildingsService.selectById(personnel.getBid());
            community = communityService.selectById(buildings1.getCid());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        map.addAttribute("buildings1",buildings1);
        map.addAttribute("community",community);
        map.addAttribute("personnel",personnel);
        return "personnel_edit";
    }
    @RequestMapping("/edit")
    public String personnel_edit(Personnel p,MultipartFile headerFile){

        String fileName = "";
        System.out.println(headerFile);
        if(!headerFile.isEmpty()){
        try {
            fileName = FileUtils.fileUpload(headerFile);


        } catch (Exception e) {
            throw new RuntimeException(e);
        }

            p.setImg(fileName);
        }

        System.out.println(p.isState());
        try {
            personnelService.updateById(p);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "redirect:/personnel/listUi";
    }
}
